<?php
/**
 * 投资管理模块
 */
namespace Admin\Controller\Customer;
use Think\Controller;
use Admin\Controller\Common\BaseController;

class CustomerController extends BaseController
{
	/**
	 *	管理后台菜单初始化与用户登录身份验证
	 */
//	public function _initialize()
//	{
//		// 判断用户是否登陆
//		if (1!=1) //	!session('?regist_id')
//		{
//			$url = 'http://' . $_SERVER['HTTP_HOST'] .'/tp_bypal/index.php';
//			$this->redirect($url);
//			// echo "<script>alert('权限不足，无法操作');window.location.href='".$url."';</script>";
//			exit;
//		}
//	}


	/**
	 * 客户管理列表页
	 */
	public function customer_manage()
    {
//        $page=I("post.page","1");
//        $rows=I("post.rows","10");
//		$nameSelect = I("post.nameSelect","");
//		$phoneSelect = I("post.phoneSelect","");
//		$emailSelect = I("post.emailSelect","");
//
//		$search_array['nameSelect'] = $nameSelect;
//		$search_array['phoneSelect'] = $phoneSelect;
//		$search_array['emailSelect'] = $emailSelect;
//        $search_array['page']=$page;
//        $search_array['rows']=$rows;
//		$this->assign("search_array",$search_array);
////        $condition=array();
//		$customer = D("Custinfo");
//		//	搜索查询条件
//		$cust_result=$customer->select_cust_info($search_array);
//        $count=$customer->select_cust_info_count($search_array);
//        $result['total']=$count;
//        $result['rows']=$cust_result;
//        $json=json_encode($cust_result);
//		$this->assign('customer_json',$json);

		$this->display();
	}

    public function customer_main(){
        $page=I("post.page","1");
        $rows=I("post.rows","10");
        $nameSelect = I("post.nameSelect","");
        $phoneSelect = I("post.phoneSelect","");
        $emailSelect = I("post.emailSelect","");

        $search_array['nameSelect'] = $nameSelect;
        $search_array['phoneSelect'] = $phoneSelect;
        $search_array['emailSelect'] = $emailSelect;
        $search_array['page']=$page;
        $search_array['rows']=$rows;
        $this->assign("search_array",$search_array);
//        $condition=array();
        $customer = D("Custinfo");
        //	搜索查询条件
        $cust_result=$customer->select_cust_info($search_array);
        $count=$customer->select_cust_info_count($search_array);
        $result['total']=$count;
        $result['rows']=$cust_result;
        $json=json_encode($result);
        echo $json;
    }


	/**
	 * 客户新增
	 */
	public function customer_add() 
	{
		//	检测用户名是否存在
		if ($_POST && array_key_exists('method', $_POST) && $_POST['method'] == 'checkLoadName') 
		{
			$loadName = $_POST['loadName'];

			$model = D("bypal_cust_info");
			$result = $model->where('cust_loadname='.$loadName)->find();
			if ($result) 
			{
				$this->ajaxReturn('exist');
			}
			else
			{
				$this->ajaxReturn('ok');
			}
		}

		if ($_POST && array_key_exists('cust_phone', $_POST) && $_POST['cust_phone']) 
		{
			$model = D('bypal_cust_info');
			$data['cust_name'] = $_POST['cust_name'];			//	姓名
			if ($_POST['cust_sex']) 
			{
				$data['cust_sex'] = $_POST['cust_sex'];			//	性别
			}
			$data['cust_identityid'] = '0';						//	证件类型ID
			$data['cust_identity'] = $_POST['cust_identity'];	//	身份证号码
			$data['cust_phone'] = $_POST['cust_phone'];			//	手机号码
			$data['cust_email'] = $_POST['cust_email'];			//	邮箱
			$data['cust_loadname'] = $_POST['cust_phone'];		//	登录名
			$data['cust_pwd'] = md5('123456');					//	密码
			$data['cust_createdate'] = time();					//	创建时间
			$data['cust_recentdate'] = time();					//	最近登录时间
			if ($result = $model->add($data)) 
			{
				//	借款余额表
				$insert_balance_borrow = D('bypal_balance_borrow');
				$balance_borrow_array['fund_balance'] = '0';
				$balance_borrow_array['last_time'] = date('Y-m-d H:i:s', time());
				$balance_borrow_array['cust_id'] = $result;
				$insert_balance_borrow->add($balance_borrow_array);

				//	投资余额表
				$insert_balance_invest = D('bypal_balance_invest');
				$balance_invest_array['fund_balance'] = '0';
				$balance_invest_array['last_time'] = date('Y-m-d H:i:s', time());
				$balance_invest_array['cust_id'] = $result;
				$insert_balance_invest->add($balance_invest_array);

				//	详细信息表
				$insert_detailinformation = D('bypal_cust_detailinformation');
				$detailinformation_array['cust_id'] = $result;
				$detailinformation_array['status'] = '0';
				$insert_detailinformation->add($detailinformation_array);

				//	联系人信息表
				$insert_linkman = D('bypal_cust_linkman');
				$linkman_array['cust_id'] = $result;
				$linkman_array['status'] = '0';
				$insert_linkman->add($linkman_array);

				//	工作信息表
				$insert_workdetail = D('bypal_cust_workdetail');
				$workdetail_array['cust_id'] = $result;
				$workdetail_array['status'] = '0';
				$insert_workdetail->add($workdetail_array);

				echo "<script>alert('新增客户成功！');window.location.href='../../customer/customer/customer_update?cust_id=".$result."';</script>";
			}
		}

		$this->display();
	}


	/**
	 * 客户编辑
	 */
	public function customer_update() 
	{
		if (IS_GET && I('get.cust_id')) 
		{
			//	基本信息数据
			$model = D("bypal_cust_info");
			$data_array = $model->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_array',$data_array);

			//	详细信息数据
			$model_detail = D("bypal_cust_detailinformation");
			$data_detail = $model_detail->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_detail',$data_detail);

			//	联系人信息数据
			$model_links = D("bypal_cust_linkman");
			$data_links = $model_links->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_links',$data_links);

			//	工作信息数据
			$model_workdetail = D("bypal_cust_workdetail");
			$data_workdetail = $model_workdetail->where('cust_id='.I('get.cust_id'))->find();
			$this->assign('data_workdetail',$data_workdetail);

			//	婚恋状况下拉框数据
			$model_marital = D("bypal_cust_marital");
			$data_marital = $model_marital->select();
			$this->assign('data_marital',$data_marital);

			//	最高学历下拉框数据
			$model_education = D("bypal_cust_education");
			$data_education = $model_education->select();
			$this->assign('data_education',$data_education);

			//	最高学位下拉框数据
			$model_degree = D("bypal_cust_degree");
			$data_degree = $model_degree->select();
			$this->assign('data_degree',$data_degree);

			//	居住状况下拉框数据
			$model_livestatus = D("bypal_cust_livestatus");
			$data_livestatus = $model_livestatus->select();
			$this->assign('data_livestatus',$data_livestatus);

			//	联系人关系下拉框数据
			$model_relation = D("bypal_cust_relation");
			$data_relation = $model_relation->select();
			$this->assign('data_relation',$data_relation);

			//	工作身份类型下拉框数据
			$model_worktype = D("bypal_cust_typename");
			$data_worktype = $model_worktype->select();
			$this->assign('data_worktype',$data_worktype);

			//	行业下拉框数据
			$model_profession = D("bypal_profession_info");
			$data_profession = $model_profession->select();
			$this->assign('data_profession',$data_profession);

			//	职业下拉框数据
			$model_position = D("bypal_cust_position");
			$data_position = $model_position->select();
			$this->assign('data_position',$data_position);

			//	职务下拉框数据
			$model_duty = D("bypal_cust_duty");
			$data_duty = $model_duty->select();
			$this->assign('data_duty',$data_duty);

			//	职称下拉框数据
			$model_title = D("bypal_cust_title");
			$data_title = $model_title->select();
			$this->assign('data_title',$data_title);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}

    public function customer_basic(){
        $cust_id=I("get.cust_id");

        $custinfo=D("Custinfo");
        $cust_array=$custinfo->get_baisc_by_id($cust_id);
        $this->assign("cust_array",$cust_array[0]);
        $this->display();
    }

    public function customer_detail(){
        $cust_id=I("get.cust_id");

        $custinfo=D("Custinfo");
        $cust_array=$custinfo->get_detail_by_id($cust_id);
        $cust_array[0]['cust_id']=$cust_id;
        $this->assign("cust_array",$cust_array[0]);
        $this->display();
    }

    public function customer_work(){
        $cust_id=I("get.cust_id");

        $custinfo=D("Custinfo");
        $cust_array=$custinfo->get_work_by_id($cust_id);
        $cust_array[0]['cust_id']=$cust_id;
        $this->assign("cust_array",$cust_array[0]);
        $this->display();
    }

    public function  customer_linkman(){
        $cust_id=I("get.cust_id");

        $custinfo=D("Custinfo");
        $cust_array=$custinfo->get_linkman_by_id($cust_id);
        $cust_array[0]['cust_id']=$cust_id;
        $this->assign("cust_array",$cust_array[0]);
        $this->display();
    }


	/**
	 * 客户编辑#####基本信息修改
	 */
	public function customer_update_base() 
	{
        $result=0;
        $data=I("post.");
		if ($data && array_key_exists('base', $data) && $data['base']) {
			$model = D("bypal_cust_info");
			$result=$model->where('cust_id='.$data['base'])->save($data);
		}
        $this->ajaxReturn($result);

	}


	/**
	 * 客户编辑#####详细信息修改
	 */
	public function customer_update_detail() 
	{
        $result=0;
        $data=I("post.");
		if ($data && array_key_exists('cust_id', $data) && $data['cust_id']) {
			$model = D("bypal_cust_detailinformation");
			if ($model->where('cust_id='.$data['cust_id'])->find()) {
				if ($data['marry_status_id']!='' && $data['communication_province']!='' && $data['communication_city'] && $data['communication_district'] && $data['communication_address']!='' && $data['communication_postcode']!='' && $data['education_status_id']!='' && $data['degree_status_id']!='' && $data['live_province']!='' && $data['live_city'] && $data['live_district'] && $data['live_address']!='' && $data['live_postcode']!='' && $data['live_status']!='')
				{
                    $data['status'] = '1';
				}
				else
				{
                    $data['status'] = '0';
				}
				$result=$model->where('cust_id='.$data['cust_id'])->save($data);
			}
		}
        $this->ajaxReturn($result);
	}


	/**
	 * 客户编辑#####联系人信息修改
	 */
	public function customer_update_links() 
	{
        $result=0;
        $data=I("post.");
		if ($data && array_key_exists('cust_id', $data) && $data['cust_id'])
		{
			$model = D("bypal_cust_linkman");
			if ($model->where('cust_id='.$data['cust_id'])->find())
			{
				if ($data['first_linkman_name']!='' && $data['first_linkman_type']!='' && $data['first_linkman_phone']!='' && $data['first_linkman_province']!='' && $data['first_linkman_city'] && $data['first_linkman_district'] && $data['first_linkman_address']!='' && $data['second_linkman_name']!='' && $data['second_linkman_type']!='' && $data['second_linkman_phone']!='')
				{
                    $data['status'] = '1';
				} else {
                    $data['status'] = '0';
				}
				$result=$model->where('cust_id='.$data['cust_id'])->save($data);
			}
            $this->ajaxReturn($result);
		}
	}


	/**
	 * 客户编辑#####工作信息修改
	 */
	public function customer_update_workinfo() 
	{
        $data=I("post.");
        $result=0;
		if ($data && array_key_exists('cust_id', $data) && $data['cust_id'])
		{
			$model = D("bypal_cust_workdetail");
			if ($model->where('cust_id='.$data['cust_id'])->find())
			{
				if ($data['current_status']!='' && $data['company_industry']!='' && $data['profession_status_id']!='' && $data['duty_status']!='' && $data['title_status']!='' && $data['company_name']!='' && $data['company_province']!='' && $data['company_city'] && $data['company_district'] && $data['company_address']!='' && $data['company_email']!='')
				{
                    $data['status'] = '1';
				}
				else
				{
                    $data['status'] = '0';
				}
                $result=$model->where('cust_id='.$data['cust_id'])->save($data);
			}
            $this->ajaxReturn($result);
		}
	}


	/**
	 * 客户详情
	 */
	public function customer_see() 
	{
		if (IS_GET && I('get.cust_id')) 
		{
//			$cust_id = I('get.cust_id');
//			$this->assign('cust_id',$cust_id);

            $cust_id=I("get.cust_id");
            $customer=D("Custinfo");
            $cust_result=$customer->select_cust_information($cust_id);
            $cust_data=$cust_result[0];
            $this->assign('cust_data',$cust_data);
			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}

    public function ajax_reference(){
        $cust_id=I("post.cust_id");
        $reference=D("Reference");
        $reference_array=$reference->reference_by_custid($cust_id);

        $this->ajaxReturn(json_encode($reference_array));
    }

    public function ajax_borrow(){
        $cust_id=I("post.cust_id");
        $borrowinfo=D("Borrowinfo");
        $borrow_array=$borrowinfo->borrow_by_custid($cust_id);

        $this->ajaxReturn(json_encode($borrow_array));
    }

    public function ajax_invest(){
        $cust_id=I("post.cust_id");
        $investdetail=D("InvestDetail");

        $invest_array=$investdetail->get_invest_detail_bycustid($cust_id);
        $this->ajaxReturn(json_encode($invest_array));
    }

    public function ajax_validate(){
        $cust_id=I("post.cust_id");
        $validate=D("bypal_impl_validate");
        $validate_result=$validate->field('id,cust_name,number,message_number,message,case when result=1 then "成功" else "失败" end result_name,
        case when type=1 then "身份认证" else "银行卡认证" end type_name,validate_time')->where("cust_id=".$cust_id)->select();
        $this->ajaxReturn(json_encode($validate_result));
    }


	/**
	 * 客户详情#####获取信息数据
	 */
	public function get_customer_see() 
	{
		if (IS_GET && I('get.cust_id')) 
		{
			//	基本信息数据
//			$model = D("bypal_cust_info");
//			$data_array = $model->where('cust_id='.I('get.cust_id'))->find();
//			$this->assign('data_array',$data_array);
//
//			//	详细信息数据
//			$model_detail = D("bypal_cust_detailinformation");
//			$data_detail = $model_detail->where('cust_id='.I('get.cust_id'))->find();
//			$this->assign('data_detail',$data_detail);
//
//			//	联系人信息数据
//			$model_links = D("bypal_cust_linkman");
//			$data_links = $model_links->where('cust_id='.I('get.cust_id'))->find();
//			$this->assign('data_links',$data_links);
//
//			//	工作信息数据
//			$model_workdetail = D("bypal_cust_workdetail");
//			$data_workdetail = $model_workdetail->where('cust_id='.I('get.cust_id'))->find();
//			$this->assign('data_workdetail',$data_workdetail);
//
//			//	婚恋状况下拉框数据
//			$model_marital = D("bypal_cust_marital");
//			$data_marital = $model_marital->where('m_id="'.$data_detail['marry_status_id'].'"')->find();
//			$this->assign('data_marital',$data_marital);
//
//			//	最高学历下拉框数据
//			$model_education = D("bypal_cust_education");
//			$data_education = $model_education->where('edu_id='.$data_detail['education_status_id'])->find();
//			$this->assign('data_education',$data_education);
//
//			//	最高学位下拉框数据
//			$model_degree = D("bypal_cust_degree");
//			$data_degree = $model_degree->where('deg_id='.$data_detail['degree_status_id'])->find();
//			$this->assign('data_degree',$data_degree);
//
//			//	居住状况下拉框数据
//			$model_livestatus = D("bypal_cust_livestatus");
//			$data_livestatus = $model_livestatus->where('li_id='.$data_detail['live_status'])->find();
//			$this->assign('data_livestatus',$data_livestatus);
//
//			//	联系人关系下拉框数据
//			$model_relation = D("bypal_cust_relation");
//			$data_relation = $model_relation->where('r_id='.$data_links['first_linkman_type'])->find();
//			$this->assign('data_relation',$data_relation);
//
//			$data_relation2 = $model_relation->where('r_id='.$data_links['second_linkman_type'])->find();
//			$this->assign('data_relation2',$data_relation2);
//
//			//	工作身份类型下拉框数据
//			$model_worktype = D("bypal_cust_typename");
//			$data_worktype = $model_worktype->where('cust_typeid='.$data_workdetail['current_status'])->find();
//			$this->assign('data_worktype',$data_worktype);
//
//			//	行业下拉框数据
//			$model_profession = D("bypal_profession_info");
//			$data_profession = $model_profession->where('id='.$data_workdetail['company_industry'])->find();
//			$this->assign('data_profession',$data_profession);
//
//			//	职业下拉框数据
//			$model_position = D("bypal_cust_position");
//			$data_position = $model_position->where('pos_id='.$data_workdetail['profession_status_id'])->find();
//			$this->assign('data_position',$data_position);
//
//			//	职务下拉框数据
//			$model_duty = D("bypal_cust_duty");
//			$data_duty = $model_duty->where('du_id='.$data_workdetail['duty_status'])->find();
//			$this->assign('data_duty',$data_duty);
//
//			//	职称下拉框数据
//			$model_title = D("bypal_cust_title");
//			$data_title = $model_title->where('ti_id='.$data_workdetail['title_status'])->find();

            $cust_id=I("get.cust_id");
            $customer=D("Custinfo");
            $cust_result=$customer->select_cust_information($cust_id);
			$this->assign('data_title',$cust_result);
			
			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 * 客户详情#####材料审核信息@附件审核
	 */
	public function reference_check() 
	{
		if (IS_GET && I('get.ref_id')) 
		{
			$model_reference = D("bypal_cust_reference");
			$data_reference = $model_reference->where('ref_id='.I('get.ref_id'))->find();
			$this->assign('data_reference',$data_reference);

			$model_custInfo = D("bypal_cust_info");
			$data_custInfo = $model_custInfo->where('cust_id='.$data_reference['ref_custid'])->find();
			$this->assign('data_custInfo',$data_custInfo);

			$model_refertype = D("bypal_cust_refertype");
			$data_refertype = $model_refertype->where('refertype_id='.$data_reference['ref_type'])->find();
			$this->assign('data_refertype',$data_refertype);

			$model_file = D("bypal_cust_referencefile");
			$array_file = $model_file->where('ref_id='.$data_reference['ref_id'])->select();
			$this->assign('array_file',$array_file);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 * 客户详情#####材料审核信息@附件审核提交
	 */
	public function reference_check_Submit() 
	{
		if ($_POST && array_key_exists('ref_id', $_POST) && $_POST['ref_id']) 
		{
			$update = D("bypal_cust_reference");
			$update->ref_point = $_POST['point'];
			$update->ref_status = $_POST['checkstatus'];
			$update->ref_conftime = time();
			if ($_POST['period']) 
			{
				$update->ref_explain = $_POST['period'];
			}
			else
			{
				$update->ref_explain = '0';
			}

			if ($update->where('ref_id='.$_POST['ref_id'])->save()) 
			{
				echo "<script>alert('恭喜您！修改成功');window.location.href='../../customer/customer/customer_see?cust_id=".$_POST['custId']."';</script>";
				exit;
			}
			else
			{
				echo "<script>alert('很遗憾！保存失败');window.location.href='../../customer/customer/customer_see?cust_id=".$_POST['custId']."';</script>";
				exit;
			}
		}
	}

}